var url = "http://127.0.0.1:8083/";

function write(obj,newListElement){
    var product = $(obj).find(".single-product-item");
    let a_image = product.find(".single-product-img").find("a");
    var price = product.find(".new-price");
    var name = product.find("h3").find("a");
    var modal = product.find(".modal-view");

    a_image.attr("data-target", newListElement["productId"]);
    product.find(".primary-image").attr("src",newListElement["productPicture"]);
    price.text("￥" + newListElement["productPrice"]);
    name.html(newListElement["productName"]);
    modal.attr("title",newListElement["productId"]);
}
function writeD(obj, newListElement){
    var product = $(obj).find(".single-product-item");
    let a_image = product.find(".single-product-img").find("a");
    var price = product.find(".new-price");
    var name = product.find("h3").find("a");
    var desc = product.find(".single-product-info").find(".pro_price").next();
    var modal = product.find(".modal-view");

    a_image.attr("data-target", newListElement["productId"]);
    product.find(".primary-image").attr("src",newListElement["productPicture"]);
    price.text("￥" + newListElement["productPrice"]);
    name.html(newListElement["productName"]);
    name.attr("data-target",newListElement["productId"]);
    desc.html(newListElement["productInfoIntroduction"]);
    modal.attr("title",newListElement["productId"]);
}
function getCookie(key){
    var cookie = document.cookie;
    var s = cookie.replace("; ","=");
    while (s.indexOf(";") != -1){
        s = s.replace("; ","=");
    }
    var strings = s.replace("; ","=").split("=");
    if (strings.indexOf(key) == -1){
        return null;
    }
    return strings[strings.indexOf(key) + 1].trim();
}

window.onload=function(){
    $(function () {
        var keyWord = getCookie("KEYWORD");
        if (keyWord == null){
            $.ajax({
                url: url + "user/primaryShop",
                type:"POST",
                async: false,
                data: {page: 1},
                success:function (data) {
                    if (data["code"] == 200) {
                        var $grid = $("#grid .row div.col-xs-12");
                        var $list = $("#list .row div.col-md-12");
                        var i = 0;
                        var j = 0;
                        var datum = data["data"];
                        $grid.each(function () {
                            if (i < datum.length){
                                $(this).attr("class","col-xs-12 col-width");
                                write(this,datum[i++]);
                            }else {
                                $(this).attr("class","col-xs-12 col-width hidden-sm");
                            }
                        });
                        $list.each(function () {
                            if (j < datum.length){
                                $(this).attr("class","col-md-12");
                                writeD(this,datum[j++]);
                            }else {
                                $(this).attr("class","col-md-12 hidden-sm");
                            }
                        });
                    }
                }
            });
        }else {
            $.ajax({
                url: url + "user/getKeyWordList",
                type:"POST",
                async: false,
                data: {
                    keyWord: keyWord,
                    page: 1
                },
                success:function (data) {
                    if (data["code"] == 200) {
                        var $grid = $("#grid .row div.col-xs-12");
                        var $list = $("#list .row div.col-md-12");
                        var i = 0;
                        var j = 0;
                        var datum = data["data"];
                        console.log(datum);
                        $grid.each(function () {
                            if (i < datum.length){
                                $(this).attr("class","col-xs-12 col-width");
                                write(this,datum[i++]);
                            }else {
                                $(this).attr("class","col-xs-12 col-width hidden-sm");
                            }
                        });
                        $list.each(function () {
                            if (j < datum.length){
                                $(this).attr("class","col-md-12");
                                writeD(this,datum[j++]);
                            }else {
                                $(this).attr("class","col-md-12 hidden-sm");
                            }
                        });
                    }
                }
            });
        }
    });

    //品牌显示
    $(function () {
        let $img = $(".client-area .container .client-owl .single-client img");

        $.ajax({
            url: url + "getBrand",
            type: "GET",
            async: false,
            success: function (data) {
                let datum = data["data"];
                var i = 0;
                $img.each(function () {
                    let datumElement = datum[i++];
                    this.src = datumElement["brandPicture"];
                    $(this).attr("alt", datumElement["brandName"]);
                })
            }
        })
    });

    $(function () {
        var primaryId = getCookie("PRIMARY");
        var $filter = $(".filter-by .select-filter #sort");
        if (primaryId == '' || primaryId == null){
            primaryId = $filter.val();
        }else {
            $filter.find("option").each(function () {
                if ($(this).val() == primaryId){
                    $(this).attr("selected",true);
                }
            })
        }

        if (primaryId == 0){
            return;
        }

        $.ajax({
            url: url + "user/primaryShop",
            type:"POST",
            async: false,
            data:{primaryId: primaryId},
            success:function (data) {
                if (data["code"] == 200) {
                    var $grid = $("#grid .row div.col-xs-12");
                    var $list = $("#list .row div.col-md-12");
                    var i = 0;
                    var j = 0;
                    var datum = data["data"];
                    $grid.each(function () {
                        if (i < datum.length){
                            $(this).attr("class","col-xs-12 col-width");
                            write(this,datum[i++]);
                        }else {
                            $(this).attr("class","col-xs-12 col-width hidden-sm");
                        }
                    });
                    $list.each(function () {
                        if (j < datum.length){
                            $(this).attr("class","col-md-12");
                            writeD(this,datum[j++]);
                        }else {
                            $(this).attr("class","col-md-12 hidden-sm");
                        }
                    });
                }
            }
        });
    });

    $(".filter-by .select-filter #sort").change(function () {
        var val = $(this).val();
        var primaryId = null;
        val = Number(val);
        if (val != 0) {
            primaryId = val;
        }

        $.ajax({
            url: url + "user/primaryShop",
            type: "POST",
            async: false,
            data: {primaryId: primaryId},
            success:function (data) {
                if (data["code"] == 200){
                    var $grid = $("#grid .row div.col-xs-12");
                    var $list = $("#list .row div.col-md-12");
                    var i = 0;
                    var j = 0;
                    var datum = data["data"];
                    $grid.each(function () {
                        if (i < datum.length){
                            $(this).attr("class","col-xs-12 col-width");
                            write(this,datum[i++]);
                        }else {
                            $(this).attr("class","col-xs-12 col-width hidden-sm");
                        }
                    });
                    $list.each(function () {
                        if (j < datum.length){
                            $(this).attr("class","col-md-12");
                            writeD(this,datum[j++]);
                        }else {
                            $(this).attr("class","col-md-12 hidden-sm");
                        }
                    });
                }
            }
        })
    });

    //点击图片及连接显示详情
    $(".single-product-item .single-product-img").find("a").click(function () {
        $.ajax({
            url:url + "user/setDetails",
            type:"POST",
            data:{id:$(this).attr("data-target")},
            success:function (data) {
                if (data["code"] == 200){
                    toPage("productDetails");
                }
            }
        })
    });

    //点击显示详情页
    $("a.modal-view").click(function () {
        var id = $(this).parent().parent().parent().parent().find("a");
        $.ajax({
            url:url + "user/showProduct",
            type: "POST",
            data:{productId:id.attr("data-target")},
            success:function (data) {
                if (data["code"] == 200) {
                    var $product = $(".modal-content .modal-body .modal-product");
                    var product_info = $product.find(".product-info");
                    var img = $product.find(".product-images").find(".main-image").find("img");
                    var price = product_info.find(".price-box").find(".price").find(".amount");
                    var remain = product_info.find(".product-detail").find(".remain");
                    var sales = product_info.find(".product-detail").find(".sales");
                    var desc = product_info.find(".quick-desc");
                    var name = product_info.find("h1");
                    var pid = product_info.find(".quick-add-to-cart").find(".cart").find("#pid");

                    var datum = data["data"];
                    var product = datum["product"];
                    var productInfo = datum["productInfo"];
                    var productSales = datum["productSales"];

                    $(img).attr("src",product["productPicture"]);
                    $(price).text("￥" + product["productPrice"]);
                    $(remain).text(product["inventoryRemain"]);
                    $(sales).text(productSales["salesCount"]);
                    $(desc).html(productInfo["productInfoIntroduction"]);
                    $(name).text(product["productName"]);
                    $(pid).val(product["productId"]);
                }
            }
        })
    });

    $(".shop-area-start .shop-item-filter .page-menu a").click(function () {
        $(this).attr("href","javascript:void(0);");
        var page = $(this).html();
        var primary = $(".select-filter #sort").val();
        var keyWord = getCookie("KEYWORD");
        var page_url;
        if (keyWord != null){
            $.ajax({
                url: url + "user/getKeyWordList",
                type:"POST",
                async: false,
                data: {
                    keyWord: keyWord,
                    page: page
                },
                success:function (data) {
                    if (data["code"] == 200) {
                        var $grid = $("#grid .row div.col-xs-12");
                        var $list = $("#list .row div.col-md-12");
                        var i = 0;
                        var j = 0;
                        var datum = data["data"];
                        $grid.each(function () {
                            if (i < datum.length){
                                $(this).attr("class","col-xs-12 col-width");
                                write(this,datum[i++]);
                            }else {
                                $(this).attr("class","col-xs-12 col-width hidden-sm");
                            }
                        });
                        $list.each(function () {
                            if (j < datum.length){
                                $(this).attr("class","col-md-12");
                                writeD(this,datum[j++]);
                            }else {
                                $(this).attr("class","col-md-12 hidden-sm");
                            }
                        });
                    }
                }
            });
        }
        if (primary == 0){
            page_url = url + "user/primaryShop";
            primary = null;
        }else {
            page_url = url + "user/primaryShop";
        }

        $.ajax({
            url: page_url,
            type:"POST",
            async: false,
            data: {
                primaryId: primary,
                page: page
            },
            success:function (data) {
                if (data["code"] == 200) {
                    var $grid = $("#grid .row div.col-xs-12");
                    var $list = $("#list .row div.col-md-12");
                    var i = 0;
                    var j = 0;
                    var datum = data["data"];
                    $grid.each(function () {
                        if (i < datum.length){
                            $(this).attr("class","col-xs-12 col-width");
                            write(this,datum[i++]);
                        }else {
                            $(this).attr("class","col-xs-12 col-width hidden-sm");
                        }
                    });
                    $list.each(function () {
                        if (j < datum.length){
                            $(this).attr("class","col-md-12");
                            writeD(this,datum[j++]);
                        }else {
                            $(this).attr("class","col-md-12 hidden-sm");
                        }
                    });
                }
            }
        });
    })
};

